package com.ibm.spatiotemp.test.request;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.databind.JsonNode;
import com.ibm.spatiotemp.datamodel.datatype.Constraints;
import com.ibm.spatiotemp.datamodel.datatype.request.BuildIdxRequest;
import com.ibm.spatiotemp.datamodel.datatype.request.QueryRequest;
import com.ibm.spatiotemp.datamodel.datatype.request.Request;
import com.ibm.spatiotemp.tools.jsonParser;

/**
 * 
 * @author xuxiaomin
 *
 */

public class RequestFormatter {
	
	public static Logger logger = Logger.getLogger(RequestFormatter.class);

	/*
	 * 
	 * 	public static QueryRequest get_TestIdxSearchRequest_KNN(){
		QueryRequest queryrequest = new QueryRequest();
		queryrequest.setDatasrcid("mockdatasrc");
		queryrequest.setRequesttype("query");
		queryrequest.setQuerytype(Constraints.QTYPE_KNN);
		queryrequest.setStartts(0);
		queryrequest.setEndts(System.currentTimeMillis());
		queryrequest.setQshape_json("{ \"type\": \"Point\", \"coordinates\": [0.0,500.0] }");
		queryrequest.setParameter("k", new Integer(1));
		return queryrequest;
	}
	
	public static QueryRequest get_TestIdxSearchRequest_Range(){
		QueryRequest queryrequest = new QueryRequest();
		queryrequest.setDatasrcid("mockdatasrc");
		queryrequest.setRequesttype("query");
		queryrequest.setQuerytype(Constraints.QTYPE_RANGE);
		queryrequest.setStartts(0);
		queryrequest.setEndts(System.currentTimeMillis());
		queryrequest.setQshape_json(
				 "{\"type\": \"Feature\",\"bbox\": [380,430,400,450],\"geometry\": {}}"
				);
		return queryrequest;
	}
	 * 
	 * 
	 * 
	 * */
	public Request buildRequestFromString(String jsonstr)
	{
		JsonNode objnode = jsonParser.fromString(jsonstr);
		String srcid = jsonParser.GetString(Request.DATASRC, objnode, "");
		String reqtype = jsonParser.GetString(Request.REQUESTTYPE, objnode, "");
		
		Request req = null;
		if(reqtype.equalsIgnoreCase(Constraints.REQUEST_TYPE_BUILDRIDX)){
			BuildIdxRequest idxreq = null;
			
			/*build request*/
			
			req = idxreq;
		}
		else if(reqtype.equalsIgnoreCase(Constraints.REQUEST_TYPE_SEARCHRIDX))
		{
			QueryRequest qreq = null;

			/*build request*/
			String querytype = jsonParser.GetString(QueryRequest.QUERYTYPE, objnode, "");
			if(querytype.equalsIgnoreCase(Constraints.QTYPE_RANGE))
			{
				qreq = new QueryRequest();
				
			}
			else if(querytype.equalsIgnoreCase(Constraints.QTYPE_KNN)){
				qreq = new QueryRequest();
				
			}
			else{
				logger.error("not supported query type:" + querytype);
				qreq = null;
			}
			req = qreq;
		}
		else
		{
			logger.error("not supported request :" + reqtype);
		}
		return req;
	}
	
}
